package com.branches.utils;

import java.util.ArrayList;
import java.util.List;

public class OutlierDetection {
    public static void main(String[] args) {
        double[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100}; // 示例数据集
        List<Double> outliers = findOutliers(data);
        System.out.println("异常值： " + outliers);
    }

    public static List<Double> findOutliers(double[] data) {
        double mean = calculateMean(data);
        double stdDev = calculateStandardDeviation(data, mean);
        double threshold = 2 * stdDev;
        List<Double> outliers = new ArrayList<>();

        for (double value : data) {
            if (Math.abs(value - mean) > threshold) {
                outliers.add(value);
            }
        }

        return outliers;
    }

    public static double calculateMean(double[] data) {
        double sum = 0;
        for (double value : data) {
            sum += value;
        }
        return sum / data.length;
    }

    public static double calculateStandardDeviation(double[] data, double mean) {
        double sum = 0;
        for (double value : data) {
            sum += Math.pow(value - mean, 2);
        }
        return Math.sqrt(sum / data.length);
    }
}